import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'
import { loginStatus } from '@/api/user'
import IndexView from '@/views/admin/IndexView.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    redirect:"/index",
    children: [
      {
        path: '/index',
        component: IndexView
      },
      
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LogniView.vue')
  },
]
// 管理员路由
const adminRoute=[
   {
        path: '/department',
        component: HomeView,
        redirect:"/department/index",
        children:[
          {
            path:"/department/index",
            component:() => import('../views/admin/departmentView.vue'),
            meta:{
              title:'系管理',
              role:1
            }

          }
        ]
      },
      {
        path: '/profession',
        component: HomeView,
        redirect:"/profession/index",
        children:[
          {
            path:"/profession/index",
            component: () => import('../views/admin/ProfessionView.vue'),
            meta:{
              title:'专业管理',
              role:2
            }

          }
        ]
      },
      {
        path: '/Class',
        component: HomeView,
        redirect:"/Class/index",
        children:[
          {
            path:"/Class/index",
            component: () => import('../views/admin/ClassView.vue'),
            meta:{
              title:'班级管理',
              role:4
            }

          }
        ]
      },
       {
        path: '/Student',
        component: HomeView,
        redirect:"/Student/index",
        children:[
          {
            path:"/Student/index",
            component: () => import('../views/admin/StudentView.vue'),
            meta:{
              title:'学生管理',
              role:8
            }

          }
        ]
      },
       {
        path: '/Teacher',
        component: HomeView,
        redirect:"/Teacher/index",
        children:[
          {
            path:"/Teacher/index",
           component: () => import('../views/admin/TeacherView.vue'),
            meta:{
              title:'教师管理',
              role:16
            }

          }
        ]
      },
        {
        path: '/Course',
        component: HomeView,
        redirect:"/Course/index",
        children:[
          {
            path:"/Course/index",
           component: () => import('../views/admin/CourseView.vue'),
            meta:{
              title:'课程管理',
              role:32
            }

          }
        ]
      },
         {
        path: '/Selection',
        component: HomeView,
        redirect:"/Selection/index",
        children:[
          {
            path:"/Selection/index",
          component: () => import('../views/admin/SelectionView.vue'),
            meta:{
              title:'选课修改',
              role:64
            }

          }
        ]
      },
       {
        path: '/Admin',
        component: HomeView,
        redirect:"/Admin/index",
        children:[
          {
            path:"/Admin/index",
          component: () => import('../views/admin/AdminView.vue'),
            meta:{
              title:'管理员管理',
              role:128
            }

          }
        ]
      },

]
// 学生路由
const studentRoute=[
   {
        path: '/courseselect',
        component: HomeView,
        redirect:"/courseselect/index",
        children:[
          {
            path:"/courseselect/index",
            component:() => import('../views/student/courseSelece.vue'),
            meta:{
              title:"选课课程"
            }
          }
        ]
      },
       {
        path: '/studentcourse',
        component: HomeView,
        redirect:"/studentcourse/index",
        children:[
          {
            path:"/studentcourse/index",
            component:() => import('../views/student/studentcourse.vue'),
            meta:{
              title:"学生课程"
            }
          }
        ]
      },
          {
        path: '/courseSearch',
        component: HomeView,
        redirect:"/courseSearch/index",
        children:[
          {
            path:"/courseSearch/index",
            component:() => import('../views/student/courseSearch.vue'),
            meta:{
              title:"课表查询"
            }
          }
        ]
      },
      {
        path: '/ScoreSelect',
        component: HomeView,
        redirect:"/ScoreSelecte/index",
        children:[
          {
            path:"/ScoreSelect/index",
            component:() => import('../views/student/ScoreSelect.vue'),
            meta:{
              title:"成绩查询"
            }
          }
        ]
      },
         {
        path: '/info',
        component: HomeView,
        redirect:"/info/index",
        children:[
          {
            path:"/info/index",
            component:() => import('../views/student/infoView.vue'),
            meta:{
              title:"信息维护"
            }
          }
        ]
      },
]
// 教师端
const teacherRoute=[
   {
        path: '/teacher',
        component: HomeView,
        redirect:"/teacher/index",
        children:[
          {
            path:"/teacher/index",
            component:() => import('../views/teacher/teacherView.vue'),
            meta:{
              title:"授课查询"
            }
          }
        ]
      },
       {
        path: '/tabletime',
        component: HomeView,
        redirect:"/tabletime/index",
        children:[
          {
            path:"/tabletime/index",
            component:() => import('../views/teacher/tabletimeView.vue'),
            meta:{
              title:"教师课表"
            }
          }
        ]
      },
      {
        path: '/Scoreteacher',
        component: HomeView,
        redirect:"/Scoreteacher/index",
        children:[
          {
            path:"/Scoreteacher/index",
            component:() => import('../views/teacher/ScoreTeacherView.vue'),
            meta:{
              title:"成绩录入"
            }
          }
        ]
      },
]
const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach(async (to, from, next) => {
  if (store.state.user.loggedIn) {
    if (to.path == '/login') {
      next('/')
    } else {
      // next()
      // 动态添加路由
      // 修改vuex的权限
      if(store.state.user.roles.length>0){
        next()
      }else{
         if(store.state.user.userType==3){
        // store.commit('user/updateRoles',adminRoute)
        // router.addRoutes(adminRoute)
        let newRoles= await store.dispatch('user/asyncRoles',adminRoute)  
        router.addRoutes(newRoles)
      } else if(store.state.user.userType==2){
        store.commit('user/updateRoles',teacherRoute)
        router.addRoutes(teacherRoute)
      }else if(store.state.user.userType==1){
          store.commit('user/updateRoles',studentRoute)
        router.addRoutes(studentRoute)
      }
      next({...to,replace:true})
      }
    }
  } else {
    let ret = await loginStatus()
    let loggedIn = ret.data.loggedIn
    if (loggedIn) {
      store.commit('user/updateLoggedIn')
      store.commit('user/updateUserType', ret.data.userType)
      store.commit('user/updatepermission',ret.data.permission)
     next({...to,replace:true})
    } else {
      if (to.path == '/login') {
        next()
      } else {
        next('/login')
      }
    }
  }
})
export default router
